package tec.stan.ling.topo.tree;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;

import tec.stan.ling.core.top.model.Device;
import tec.stan.ling.core.top.model.NE;
import tec.stan.ling.core.top.model.Subnet;
import tec.stan.ling.topo.ICons;
import tec.stan.ling.topo.TopoPlugin;

/**
 * 
 *
 * @author Stan 张新潮
 * @date Jan 14, 2010
 */
public class ElementTreeProvider extends LabelProvider implements
		ITreeContentProvider {

	// ITreeContentProvider start

	@Override
	public Object[] getChildren(Object parentElement) {
		if (parentElement instanceof Subnet) {
			return ((Subnet) parentElement).getChildren().toArray();
		}
		return null;
	}

	@Override
	public Object getParent(Object element) {
		return null;
	}

	@Override
	public boolean hasChildren(Object element) {
		if (element instanceof Subnet) {
			return ((Subnet) element).getChildren().size() > 0;
		}
		return false;
	}

	@Override
	public Object[] getElements(Object inputElement) {
		if (inputElement instanceof Subnet) {
			return ((Subnet) inputElement).getChildren().toArray();
		}
		return null;
	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
	}

	// ITreeContentProvider end

	public String getText(Object element) {
		if (element instanceof NE) {
			return ((NE) element).getName();
		}
		return "";
	}

	public Image getImage(Object element) {
		if (element instanceof Subnet) {
			return TopoPlugin.getImage(ICons.SUBNET_TREE);
		} else if (element instanceof Device) {
			return TopoPlugin.getImage(ICons.ELEMENT_TREE);
		}
		return null;
	}
}
